package com.xiaoming.freshmenapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.xiaoming.freshmenapp.R;
import com.xiaoming.freshmenapp.bean.MyBean;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView homeBack;
    private TextView homeTitle;
    private EditText etName;
    private EditText etPassword;
    private CheckBox cbJz;
    private Button btLogin;
    private TextView tvWjmm;
    private TextView tvRegist;
    private String name;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        homeBack = (ImageView) findViewById(R.id.home_back);

        homeTitle = (TextView) findViewById(R.id.home_title);
        homeTitle.setText("用户登录");
        etName = (EditText) findViewById(R.id.et_name);
        etPassword = (EditText) findViewById(R.id.et_password);
        cbJz = (CheckBox) findViewById(R.id.cb_jz);
        btLogin = (Button) findViewById(R.id.bt_login);
        tvWjmm = (TextView) findViewById(R.id.tv_wjmm);
        tvRegist = (TextView) findViewById(R.id.tv_regist);

        btLogin.setOnClickListener(this);
        tvRegist.setOnClickListener(this);
        tvWjmm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                submit();
                getData();
                break;
            case R.id.tv_regist:
                startActivity(new Intent(getApplicationContext(),RegistActivity.class));
                break;
            case R.id.tv_wjmm:
                startActivity(new Intent(getApplicationContext(),AddMessagesActivity.class));
                break;
        }
    }

    private boolean submit() {
        // validate
        name = etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return false;
        }

        password = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void getData() {

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.2.64.83:8080/app1/user/login";
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("name",name);
            jsonObject.put("password",password);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: "+jsonObject.toString());
                Gson gson = new Gson();
                MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);
                if (myBean.isSuccess()){
                    Toast.makeText(LoginActivity.this, "成功", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplicationContext(),ClassmateListActivity.class));
                }else {
                    Toast.makeText(LoginActivity.this, "失败", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, "网络错误"+volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);
    }
}